$(function(){
	$(".team-list").click(function(){
		var seasonId = $("#seasonId").val();
		var id = $(this).attr("id");
		window.location.href = "/competitionTeam/addPage.htm?seasonId="+seasonId+"&id="+id;
	});
	
	removeTeam();
	removeCompetition();
	
	$(".search-btn").click(function(){
		var param = $("#searchForm").serialize();
		$.ajax({
            url:'/competitionTeam/findNoCompetitionTeam.json?'+ param,
            type:'POST',
            dataType:"json",
            success:function(data) {
            	var tr_str = "<tr>"+
                       			"<td>球队id</td>"+
                       			"<td>学校</td>"+
                       			"<td>球队名</td>"+
                       		"</tr>";
            	$(data).each(function(i,obj){
            		var competitionTeamId = obj.id;
            		var competitionSchoolName = obj.schoolName;
            		var competitionTeamName = obj.name;
            		tr_str += "<tr class='team-tr'>"+
								"<td name='teamId'>"+competitionTeamId+"</td>"+
								"<td name='schoolName'>"+competitionSchoolName+"</td>"+
								"<td name='teamName'>"+competitionTeamName+"</td>"+
							"</tr>";
            	});
                $(".team-table").empty().append(tr_str);
                removeTeam();
            }
        });
	});
	
	$("#addForm").validate( {
        submitHandler : function(form) {
        	var lenth = $(".competition-team-table tr").length;
        	if(lenth > 1){
        		$("#addForm").ajaxSubmit(function(date){
	                if(date){
	                    alert("操作成功");
	                    var seasonId = $("[name='seasonId']").val();
	                    window.location.href = "/competitionTeam/list.htm?seasonId="+seasonId;
	                }else{
	                	alert("操作失败");
	                }
	            });
        		
        	}else{
        		alert("请选择参赛球队");
        	}
        },
        highlight: function (e) {
            $(e).closest('.control-group').removeClass('info').addClass('error');
        }
    });
	
	$(".rest-but").click(function(){
		var seasonId = $("[name='seasonId']").val();
        window.location.href = "/competitionTeam/list.htm?seasonId="+seasonId;
	});
	
	$(".btn-competition-team").click(function(){
		var seasonId = $("#seasonId").val();
        window.location.href = "/competitionTeam/list.htm?seasonId="+seasonId;
	});
	
	$(".btn-preliminary-contest").click(function(){
		var seasonId = $("#seasonId").val();
        window.location.href = "/preliminaryContest/list.htm?seasonId="+seasonId+"&type=1";
	});
	
	$(".btn-eliminate-contest").click(function(){
		var seasonId = $("#seasonId").val();
        window.location.href = "/preliminaryContest/list.htm?seasonId="+seasonId+"&type=2";
	});
	
	$(".export-btn").click(function(){
		window.location.href = "/competitionTeam/createTeamTable.json";
	});
	
	$(".import-btn").click(function(){
		$('#import-model').modal('show');
	});
	
	//提交更新
    /*$("#importTeamForm").validate( {
        submitHandler : function(form) {                	
            $("#importTeamForm").ajaxSubmit(function(date,res,me){
//                if(date){
                    alert("导入成功");
                    $(".close").click();
                    var seasonId = $("#seasonId").val();
                    window.location.href = "/competitionTeam/list.htm?seasonId="+seasonId;
//                }
            });
        },
        highlight: function (e) {
            $(e).closest('.control-group').removeClass('info').addClass('error');
        }
    });*/
	
	/*$(".btn-primary").click(function(){
		
		$("#importTeamForm").submit();
	});*/
	
	$('#id-input-file').ace_file_input({
        no_file: '没有文件 ...',
        btn_choose: 'Choose',
        btn_change: 'Change',
        droppable: false,
        onchange: null,
        thumbnail: false
    });
});

var removeTeam = function(){
	$(".team-tr").unbind().bind("click",function(){
		var teamId = $(this).find("[name='teamId']").text();
		var schoolName = $(this).find("[name='schoolName']").text();
		var teamName = $(this).find("[name='teamName']").text();
		var lenth = $(".competition-team-table tr").length;
		if(lenth >= 5){
			alert("最多四支球队");
		}else{
			$(this).remove();
			var tr_str = "<tr class='competition-team-tr'>"+
							"<td>"+
								"<span name='number'>"+lenth+"</span>"+
								"<input type='hidden' name='number' value='"+lenth+"'/>"+
							"</td>"+
							"<td>"+
								"<span name='competitionTeamId'>"+teamId+"</span>"+
								"<input type='hidden' name='teamId' value='"+teamId+"'/>"+
							"</td>"+
							"<td>"+
								"<span name='competitionSchoolName'>"+schoolName+"</span>"+
							"</td>"+
							"<td>"+
								"<span name='competitionTeamName'>"+teamName+"</span>"+
							"</td>"+
						"</tr>";
			$(".competition-team-table").append(tr_str);
			removeCompetition();
		}
	});
};

var removeCompetition = function(){
	$(".competition-team-tr").unbind().bind("click",function(){
		var competitionTeamId = $(this).find("[name='competitionTeamId']").text();
		var competitionSchoolName = $(this).find("[name='competitionSchoolName']").text();
		var competitionTeamName = $(this).find("[name='competitionTeamName']").text();
		$(this).remove();
		var number = $(this).find("[name='number']").text();
		$("[name='number']").each(function(i,obj){
			var num = $(obj).text();
			if(num > number){
				$(obj).text(--num);
			}
		});
		var tr_str = "<tr class='team-tr'>"+
						"<td name='teamId'>"+competitionTeamId+"</td>"+
						"<td name='schoolName'>"+competitionSchoolName+"</td>"+
						"<td name='teamName'>"+competitionTeamName+"</td>"+
					"</tr>";
		$(".team-table").append(tr_str);
		removeTeam();
	});
};